package com.niuwa.p2p.action.producttemplate;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.struts2.convention.annotation.Action;
import org.springframework.beans.factory.annotation.Autowired;

import com.niuwa.p2p.action.JsonAction;
import com.niuwa.p2p.core.mvc.entity.JsonResult;
import com.niuwa.p2p.core.mvc.entity.PageBean;
import com.niuwa.p2p.core.utils.DictConstant;
import com.niuwa.p2p.entity.productTemplate.AreaPropCfg;
import com.niuwa.p2p.entity.productTemplate.GuaranteeUnit;
import com.niuwa.p2p.entity.productTemplate.ProductProtocol;
import com.niuwa.p2p.entity.productTemplate.ProductTemplate;
import com.niuwa.p2p.opr.utils.SessionUtil;
import com.niuwa.p2p.service.guarantee.IGuaranteeUnitService;
import com.niuwa.p2p.service.productTemplate.IInvestProdTemplateService;
import com.niuwa.p2p.service.protocol.IProtocolService;
import com.niuwa.p2p.vo.productTemplate.InvestProdTemplateVo;
import com.niuwa.p2p.vo.productTemplate.InvestProdTmptForm;
import com.niuwa.p2p.vo.productTemplate.InvestProdTmptVo;
import com.niuwa.p2p.vo.protocol.ProductProtocolVo;
import com.opensymphony.xwork2.ActionContext;

public class InvestProductTmpltOperAction extends JsonAction {
	
	/**
	 * 
	 */
	private static final long serialVersionUID = -8945193300593716315L;
	/** 产品模板Servcie spring扫包注入 */
	@Autowired
	private IInvestProdTemplateService investProdTemplateService;
	
	/** 产品模板ID */
	private long tempId;
	
	/** 拒绝理由 */
	private String rejectReason;
	
	private InvestProdTmptForm ppv;
	
	@Autowired
	private IProtocolService protocolService;
	
	/** 协议名称*/
	private String protName;
	
	/** 协议主版本号*/
	private String mainVersion;
	
	/** 协议小版本号*/
	private String minorVersion;
	
	private ProductProtocolVo productProtocolVo;
	
	private List<AreaPropCfg> apcList = new ArrayList<AreaPropCfg>();
	
	private List<ProductProtocol> ppList = new ArrayList<ProductProtocol>();

	private String tempType;//模板类型
	private String tempName;//模板名称
	
	@Autowired
	private IGuaranteeUnitService guarantService;
	
	public long getTempId() {
		return tempId;
	}

	public void setTempId(long tempId) {
		this.tempId = tempId;
	}

	public String getRejectReason() {
		return rejectReason;
	}

	public void setRejectReason(String rejectReason) {
		this.rejectReason = rejectReason;
	}

	/**
	 * 跳转到账户操作视图
	 * 
	 * @return 账户操作视图
	 * 
	 */	
	@Action("gotoInvestProductTemplate")
	public String gotoInvestProductTemplate() {
		return "investProductTemplate";
	}
	
	/**
	 * 产品列表分页查询
	 * @return
	 */
 	@Action("listProductTemplt")
 	public String listProductTemplt() {
		// 获取Grid条件选择参数
		Map<String, Object> paramMap = getParamMap();
		//产品模板类型：投资
		paramMap.put("tempType", DictConstant.TempType.INVEST);
		// 执行查询操作
		PageBean<InvestProdTemplateVo> pagination = investProdTemplateService.search(paramMap);
		// 设置返回
		setObjectReSult(pagination);
		return NONE;
 	}
 	
 	/**
 	 * 跳转到借款模板列表页面
 	 * @return
 	 */
 	@Action("gotoLoanProductTemplate")
	public String gotoLoanProductTemplate() {
		return "borrowProductTemplate";
	}
 	
 	/**
	 * 借款模板列表页面分页查询
	 * @return
	 */
 	@Action("loanListProductTemplt")
 	public String loanlistProductTemplt() {
		// 获取Grid条件选择参数
		Map<String, Object> paramMap = getParamMap();
		//产品模板类型：借款
		paramMap.put("tempType", DictConstant.TempType.LOAN);
		// 执行查询操作
		PageBean<InvestProdTemplateVo> pagination = investProdTemplateService.search(paramMap);
		// 设置返回
		setObjectReSult(pagination);
		return NONE;
 	}
 	
 	/**
 	 * @return 债权转让模板列表
 	 */
 	@Action("listTransProdTemplt")
 	public String listTransProdTemplt() {
 		Map<String, Object> paramMap = getParamMap();
 		paramMap.put("tempType", DictConstant.TempType.TRANSFER);
 		PageBean<InvestProdTemplateVo> pagination = investProdTemplateService.search(paramMap);
 		setObjectReSult(pagination);
 		return NONE;
 	}
 	
 	/**
	 * 删除产品模板 
	 * 
	 * @return
	 * success: 操作成功
	 * fail:    后台Message(失败,产品模板不存在)
	 * @throws Exception 
	 */
	@Action("deleteProductTmplt")
	public String deleteProductTmplt() throws Exception {
		JsonResult result = new JsonResult();		
		int num = investProdTemplateService.deleteProductTmplt(tempId);	
		if(num > 0){
			result.setSuccess(true);
			result.setMsg("操作成功");
		}else{
			result.setSuccess(false);
			result.setMsg("该模板不能删除");
		}
		this.setResult(result);
		return NONE;
	}

 	/**
	 * 产品模板审批通过 
	 * 
	 * @return
	 * success: 操作成功
	 * fail:    后台Message(失败,产品模板不存在)
	 * @throws Exception 
	 */
	@Action("passAudit")
	public String passAudit() throws Exception {
		JsonResult result = new JsonResult();		
		investProdTemplateService.updateProductTmpltToPass(tempId, SessionUtil.getSessionUser().getId());
		result.setSuccess(true);
		result.setMsg("操作成功");
		this.setResult(result);
		return NONE;
	}

	
 	/**
	 * 产品模板审批拒绝 
	 * 
	 * @return
	 * success: 操作成功
	 * fail:    后台Message(失败,产品模板不存在)
	 * @throws Exception 
	 */
	@Action("rejectAudit")
	public String rejectAudit() throws Exception {
		JsonResult result = new JsonResult();		
		investProdTemplateService.updateProductTmpltToReject(tempId, rejectReason, SessionUtil.getSessionUser().getId());		
		result.setSuccess(true);
		result.setMsg("操作成功");
		this.setResult(result);
		return NONE;
	}
	
	/**
	 * 添加投资产品模板
	 * 
	 * @return String
	 */
	@Action("addInvestProdTemplate")
	public String addInvestProdTemplate(){
		
		JsonResult result = new JsonResult();
		
		// 获取用户ID
		long userId = SessionUtil.getSessionUser().getId();
		
		try {
			ProductTemplate productTemplate = new ProductTemplate();
			productTemplate.setTempName(ppv.getTempName());
			productTemplate.setMainVersion(Integer.parseInt(ppv.getMainVersion()));
			productTemplate.setMinorVersion(Integer.parseInt(ppv.getMinorVersion()));
			
			/*判断是否存在模版名称、版本号比输入的版本号高的模版*/
			int findProductTmptByName = investProdTemplateService.findProductTmptByName(productTemplate);
			if (findProductTmptByName > 0) {
				result.setSuccess(false);
				result.setMsg("版本号不允许重复，新版本不得低于旧版本");
			}else {
				investProdTemplateService.addInestProductTmplt(userId, ppv);
				result.setSuccess(true);
				result.setMsg("操作成功");
			}
		} catch (Exception e) {
			result.setSuccess(false);
			result.setMsg("操作失败");
		}
		this.setResult(result);
		return NONE;
	}
	
	/**
	 * 修改产品模板
	 * 
	 * @return
	 */
	@Action("updateInvestProdTemplate")
	public String updateInvestProdTemplate(){
		
		ActionContext context = ActionContext.getContext();

		List<GuaranteeUnit> guList = guarantService.searchAll();
		context.put("guList", guList);
		
		InvestProdTmptVo prod = investProdTemplateService.getProdTmptById(tempId);
		
		ppv = prod.getTmptForm();
		context.put("ppv", ppv);

		apcList = prod.getApcList();
		ActionContext.getContext().put("apcList", apcList);
		
		ppList = prod.getPpList();
		ActionContext.getContext().put("ppList", ppList);
		
		List<ProductProtocolVo> protocols = protocolService.selectAllProtocol();
		ActionContext.getContext().put("protocols", protocols);
		
		return "addInvestProdTemplate";
	}
	
	/**
	 * 查看产品模板
	 * 
	 * @return
	 */
	@Action("viewInvestProdTemplate")
	public String viewInvestProdTemplate() {
		
		ActionContext context = ActionContext.getContext();

		List<GuaranteeUnit> guList = guarantService.searchAll();
		context.put("guList", guList);
		
		InvestProdTmptVo prod = investProdTemplateService.getProdTmptById(tempId);
		
		ppv = prod.getTmptForm();
		ppv.setMinorVersion(String.valueOf(Long.valueOf(ppv.getMinorVersion()) - 1));
		context.put("ppv", ppv);

		apcList = prod.getApcList();
		context.put("apcList", apcList);
		
		ppList = prod.getPpList();
		context.put("ppList", ppList);

		return "viewInvestProdTemplate";
		
	}
			
	
	
	/**
	 * 跳转到添加投资产品模板视图
	 * 
	 * @return 投资产品模板视图
	 * 
	 */	
	@Action("gotoAddInvestProdTemplate")
	public String gotoAddInvestProdTemplate() {

		//获取所有协议信息
		List<ProductProtocolVo> protocols = protocolService.selectAllProtocol();
		ActionContext.getContext().put("protocols", protocols);
		
		List<GuaranteeUnit> guList = guarantService.searchAll();
		ActionContext.getContext().put("guList", guList);
		
		return "addInvestProdTemplate";
	}
	
	/**
	 * 根据协议名称获取版本号
	 * @return
	 */
	@Action("getVersionList")
	public String getVersionList() {
		List<ProductProtocolVo> protocolVersions = protocolService.selectProtocolVersionByName(protName);
		setObjectReSult(protocolVersions);
		return NONE;
	}
	
	/**
	 * 根据协议名称，主版本号，小版本号获取协议信息
	 * @return
	 */
	@Action("getProtocolInfo")
	public String getProtocolInfo() {
		ProductProtocolVo vo = new ProductProtocolVo();
		vo.setProtName(protName);
		vo.setMainVersion(Integer.valueOf(mainVersion));
		vo.setMinorVersion(Integer.valueOf(minorVersion));
		productProtocolVo = protocolService.selectProtocolByNameAndVersion(vo);
		setObjectReSult(productProtocolVo);
		return NONE;
	}
	
	/**************************************lijian start****************************************/
	/**
	 * 根据产品模板类型、状态获取模板信息
	 * @return
	 */
	@Action("findInvestProdTempByCondition")
	public String findInvestProdTempByCondition() {
		List<ProductTemplate> templates = investProdTemplateService.findInvestProdTempByCondition(tempType);
		setObjectReSult(templates);
		return NONE;
	}
	
	/**
	 * 根据产品模板名称获取模板信息
	 * @return
	 */
	@Action("findProdVersionByCondition")
	public String findProdVersionByCondition() {
		List<InvestProdTemplateVo> templates = investProdTemplateService.findProdVersionByCondition(tempName);
		setObjectReSult(templates);
		return NONE;
	}
	
	
	/**
	 * 获取投资产品模板信息
	 * @return
	 */
	@Action("findInvestProdTemplateByCondition")
	public String findInvestProdTemplateByCondition() {
		setObjectReSult(investProdTemplateService.findInvestProdTemplateByCondition(tempId));
		return NONE;
	}
	/**************************************lijian end****************************************/
	
	public InvestProdTmptForm getPpv() {
		return ppv;
	}

	public void setPpv(InvestProdTmptForm ppv) {
		this.ppv = ppv;
	}

	public String getProtName() {
		return protName;
	}

	public void setProtName(String protName) {
		this.protName = protName;
	}

	public String getMainVersion() {
		return mainVersion;
	}

	public void setMainVersion(String mainVersion) {
		this.mainVersion = mainVersion;
	}

	public String getMinorVersion() {
		return minorVersion;
	}

	public void setMinorVersion(String minorVersion) {
		this.minorVersion = minorVersion;
	}

	public ProductProtocolVo getProductProtocolVo() {
		return productProtocolVo;
	}

	public void setProductProtocolVo(ProductProtocolVo productProtocolVo) {
		this.productProtocolVo = productProtocolVo;
	}

	public List<AreaPropCfg> getApcList() {
		return apcList;
	}

	public void setApcList(List<AreaPropCfg> apcList) {
		this.apcList = apcList;
	}

	public List<ProductProtocol> getPpList() {
		return ppList;
	}

	public void setPpList(List<ProductProtocol> ppList) {
		this.ppList = ppList;
	}

	public String getTempType() {
		return tempType;
	}

	public void setTempType(String tempType) {
		this.tempType = tempType;
	}

	public String getTempName() {
		return tempName;
	}

	public void setTempName(String tempName) {
		this.tempName = tempName;
	}
}
